package com.rkc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rkc.enums.AddressStatusEnum;
import com.rkc.interceptor.LoginInterceptor;
import com.rkc.mapper.AddressMapper;
import com.rkc.model.AddressDO;
import com.rkc.model.LoginUser;
import com.rkc.request.AddressAddRequest;
import com.rkc.service.AddressService;
import com.rkc.vo.AddressVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author `RKC`
 * @date 2021/10/7 16:40
 */
@Slf4j
@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressMapper addressMapper;

    /**
     * 查找用户全部收货地址
     * @return
     */
    @Override
    public List<AddressVO> listUserAllAddresses() {
        LoginUser loginUser = LoginInterceptor.THREAD_LOCAL.get();
        List<AddressDO> addressDOList = addressMapper.selectList(new QueryWrapper<AddressDO>().eq("user_id", loginUser.getId()));
        List<AddressVO> addressVOList = addressDOList.stream().map(obj -> {
            AddressVO addressVO = new AddressVO();
            BeanUtils.copyProperties(obj, addressVO);
            return addressVO;
        }).collect(Collectors.toList());
        return addressVOList;
    }

    @Override
    public AddressVO detail(Long id) {
        LoginUser loginUser = LoginInterceptor.THREAD_LOCAL.get();
        AddressDO addressDO = addressMapper.selectOne(new QueryWrapper<AddressDO>().eq("id", id).eq("user_id", loginUser.getId()));
        if (addressDO != null) {
            AddressVO addressVO = new AddressVO();
            BeanUtils.copyProperties(addressDO, addressVO);
            return addressVO;
        }
        return null;
    }

    /**
     * 新增收货地址
     * @param addressAddRequest
     * @return
     */
    @Override
    public int add(AddressAddRequest addressAddRequest) {
        LoginUser loginUser = LoginInterceptor.THREAD_LOCAL.get();
        AddressDO addressDO = new AddressDO();
        addressDO.setCreateTime(new Date());
        addressDO.setUserId(loginUser.getId());
        BeanUtils.copyProperties(addressAddRequest, addressDO);
        //是否有默认收货地址
        if (addressAddRequest.getDefaultStatus() == 1) {
            //查找数据库是否有默认收货地址
            AddressDO defaultAddressDO = addressMapper.selectOne(new QueryWrapper<AddressDO>().eq("user_id",
                    loginUser.getId()).eq("default_status", AddressStatusEnum.DEFAULT_STATUS.getStatus()));
            if (defaultAddressDO != null) {
                log.info("旧的默认地址：{}", defaultAddressDO.toString());
                //需要修改默认地址
                defaultAddressDO.setDefaultStatus(AddressStatusEnum.COMMON_STATUS.getStatus());
                addressMapper.update(defaultAddressDO, new QueryWrapper<AddressDO>().eq("id", defaultAddressDO.getId()));
            }
        }
        int row = addressMapper.insert(addressDO);
        log.info("新增收货地址：{}", row);
        return row;
    }

    /**
     * 删除指定收货地址
     * @param addressId
     * @return
     */
    @Override
    public int delete(int addressId) {
        LoginUser loginUser = LoginInterceptor.THREAD_LOCAL.get();
        return addressMapper.delete(new QueryWrapper<AddressDO>().eq("id", addressId).eq("user_id", loginUser.getId()));
    }
}
